package com.citywy.config;

import com.citywy.controller.interceptor.ProjectInterceptor;
import com.citywy.controller.interceptor.ProjectInterceptor2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ComponentScan({"com.citywy.controller"})
@EnableWebMvc
//实现WebMvcConfigure接口简化开发，不需要SpringMvcSupport了，但具有一定的侵入性
public class SpringMvcConfig implements WebMvcConfigurer {
    @Autowired
    private ProjectInterceptor projectInterceptor;
    @Autowired
    private ProjectInterceptor2 projectInterceptor2;

    //设置静态资源访问过滤，当前类需要设置为配置类，并被扫描加载
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //当访问pages/***时候，直接从该目录下查找内容
        registry.addResourceHandler("/pages/**").addResourceLocations("/assets/pages/");
        registry.addResourceHandler("/js/**").addResourceLocations("/assets/js/");
        registry.addResourceHandler("/css/**").addResourceLocations("/assets/css/");
        registry.addResourceHandler("/plugins/**").addResourceLocations("/assets/plugins/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //配置拦截器
        registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*");
        registry.addInterceptor(projectInterceptor2).addPathPatterns("/books","/books/*");
    }
}
